<!--
 * @Description: 
 * @Author: dh
 * @Date: 2021-08-19 17:18:39
 * @LastEditors: dh
 * @LastEditTime: 2021-09-15 14:58:28
-->
<!doctype html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>

	<body>
		<!-- 方案一 利用伪元素 -->
		<img src="../assets/img/02.jpg" alt="失败" onerror="this.classList.add('error')" />

		<!-- 方案二 失败显示本地默认图，如果本地图也不存在，解决无限循环 -->
		<!-- <img src="image.gif" onerror="javascript:this.src='src/images/default.jpg';this.onerror = null"> -->
		<style>
			img {
				width: 200px;
				height: 400px;
			}

			img.error {
				position: relative;
				display: inline-block;
			}

			img.error::before {
				content: '';
				/** 定位代码 **/
				background: url('../assets/img/天空.jpg') no-repeat;
				position: absolute;
				top: 0;
				bottom: 0;
				left: 0;
				right: 0;
				margin: auto;
			}

			img.error::after {
				content: attr(alt);
				/** 定位代码 **/
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				color: #fff;
				text-align: center;
				background: #000;
				opacity: 0.5;
			}
		</style>
		<script>
			function err(e) {}
		</script>
	</body>
</html>
